package com.taozi.question.service.Impl;

import com.taozi.common.exception.base.BaseException;
import com.taozi.common.utils.DateUtils;
import com.taozi.common.utils.SecurityUtils;
import com.taozi.common.utils.uuid.IdUtils;
import com.taozi.question.domain.QuestionInfo;
import com.taozi.question.mapper.QuestionInfoMapper;
import com.taozi.question.service.IQuestionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 问题详情Service业务层处理
 *
 * @author taozi
 * @date 2024-06-03
 */
@Service
public class QuestionInfoServiceImpl implements IQuestionInfoService {
    @Autowired
    private QuestionInfoMapper questionInfoMapper;

    /**
     * 查询问题详情
     *
     * @param id 问题详情主键
     * @return 问题详情
     */
    @Override
    public QuestionInfo selectQuestionInfoById(String id) {
        return questionInfoMapper.selectQuestionInfoById(id);
    }

    /**
     * 查询问题详情列表
     *
     * @param questionInfo 问题详情
     * @return 问题详情
     */
    @Override
    public List<QuestionInfo> selectQuestionInfoList(QuestionInfo questionInfo) {
        return questionInfoMapper.selectQuestionInfoList(questionInfo);
    }

    /**
     * 新增问题详情
     *
     * @param questionInfo 问题详情
     * @return 结果
     */
    @Override
    public int insertQuestionInfo(QuestionInfo questionInfo) {
        questionInfo.setCreateBy(SecurityUtils.getNickName());
        questionInfo.setQuestionId(getCountNum() + 1);
        questionInfo.setId(IdUtils.fastSimpleUUID());
        questionInfo.setCreateTime(DateUtils.getNowDate());
        return questionInfoMapper.insertQuestionInfo(questionInfo);
    }

    /**
     * 修改问题详情
     *
     * @param questionInfo 问题详情
     * @return 结果
     */
    @Override
    public int updateQuestionInfo(QuestionInfo questionInfo) {
        questionInfo.setUpdateTime(DateUtils.getNowDate());
        return questionInfoMapper.updateQuestionInfo(questionInfo);
    }

    /**
     * 批量删除问题详情
     *
     * @param ids 需要删除的问题详情主键
     * @return 结果
     */
    @Override
    public int deleteQuestionInfoByIds(String[] ids) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUserId())){
            throw new BaseException("只有管理员才能进行删除操作噢~");
        }
        return questionInfoMapper.deleteQuestionInfoByIds(ids);
    }

    /**
     * 删除问题详情信息
     *
     * @param id 问题详情主键
     * @return 结果
     */
    @Override
    public int deleteQuestionInfoById(String id) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUserId())){
            throw new BaseException("只有管理员才能进行删除操作噢~");
        }
        return questionInfoMapper.deleteQuestionInfoById(id);
    }

    private int getCountNum() {
        return questionInfoMapper.countNum();
    }
}
